//
//  RXTabBar.swift
//  weibo
//
//  Created by uncle-R on 15/12/15.
//  Copyright © 2015年 uncle-R. All rights reserved.
//

import UIKit

class RXTabBar: UITabBar {

    
    private let count = 5
    //懒加载加号按钮.
    lazy var plusBtn :UIButton = {
        let button  = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState:UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //代码块中不能省略self
        self.addSubview(button)
        return button
    }()
     
    //重新布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算tabbarButton的宽度
        let width = self.bounds.width / CGFloat(count)
        
        var index = 0
        // 最左边的frame
        let frame = CGRectMake(0, 0, width, self.bounds.height)
        
        //遍历子控件
        
        for view in self.subviews{
            
            if view is UIControl && !(view is UIButton){
                
                view.frame = CGRectOffset(frame, CGFloat(index) * width, 0)
                
                index += index == 1 ? 2 : 1
            }
        }
        
        
        plusBtn.frame = CGRectOffset(frame, 2.0 * width, 0)
    }
    

}
